package com.xyz.keshe;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.xyz.keshe.adapter.CommentAdapter;
import com.xyz.keshe.adapter.NewsAdapter;
import com.xyz.keshe.dao.CommentDao;
import com.xyz.keshe.dao.NewsDao;
import com.xyz.keshe.model.Comment;
import com.xyz.keshe.model.News;

import java.util.List;

public class NewsDetailActivity extends AppCompatActivity {

    private TextView tvTitle;
    private TextView tvContent;
    private TextView tvAuthor;
    private TextView tvCategory;
    private TextView tvDate;
    private TextView tvViewCount;
    private ListView lvRelatedNews;
    private ListView lvComments;
    private EditText etComment;
    private EditText etUsername;
    private Button btnSubmitComment;
    private Button btnBack;

    private NewsDao newsDao;
    private CommentDao commentDao;
    private News news;
    private List<News> relatedNews;
    private List<Comment> comments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);

        // 初始化视图
        tvTitle = findViewById(R.id.tv_title);
        tvContent = findViewById(R.id.tv_content);
        tvAuthor = findViewById(R.id.tv_author);
        tvCategory = findViewById(R.id.tv_category);
        tvDate = findViewById(R.id.tv_date);
        tvViewCount = findViewById(R.id.tv_view_count);
        lvRelatedNews = findViewById(R.id.lv_related_news);
        lvComments = findViewById(R.id.lv_comments);
        etComment = findViewById(R.id.et_comment);
        etUsername = findViewById(R.id.et_username);
        btnSubmitComment = findViewById(R.id.btn_submit_comment);
        btnBack = findViewById(R.id.btn_back);

        // 初始化DAO
        newsDao = new NewsDao(this);
        commentDao = new CommentDao(this);

        // 获取新闻ID
        Intent intent = getIntent();
        int newsId = intent.getIntExtra("news_id", 0);
        if (newsId == 0) {
            Toast.makeText(this, "新闻ID无效", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 获取新闻详情
        news = newsDao.getNewsById(newsId);
        if (news == null) {
            Toast.makeText(this, "新闻不存在", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 增加浏览量
        newsDao.incrementViewCount(newsId);
        news.setViewCount(news.getViewCount() + 1);

        // 设置新闻详情
        tvTitle.setText(news.getTitle());
        tvContent.setText(news.getContent());
        tvAuthor.setText("作者：" + news.getAuthor());
        tvCategory.setText("栏目：" + news.getCategoryName());
        tvDate.setText("发布时间：" + news.getCreateTime());
        tvViewCount.setText("浏览量：" + news.getViewCount());

        // 获取相关新闻
        relatedNews = newsDao.getRelatedNews(newsId, news.getCategoryId(), 5);
        NewsAdapter relatedNewsAdapter = new NewsAdapter(this, relatedNews);
        lvRelatedNews.setAdapter(relatedNewsAdapter);

        // 设置相关新闻点击事件
        lvRelatedNews.setOnItemClickListener((parent, view, position, id) -> {
            News clickedNews = (News) parent.getItemAtPosition(position);
            Intent newsIntent = new Intent(NewsDetailActivity.this, NewsDetailActivity.class);
            newsIntent.putExtra("news_id", clickedNews.getId());
            startActivity(newsIntent);
            finish();
        });

        // 获取评论
        loadComments();

        // 设置返回按钮点击事件
        btnBack.setOnClickListener(v -> finish());

        // 设置提交评论按钮点击事件
        btnSubmitComment.setOnClickListener(v -> submitComment());
    }

    private void loadComments() {
        comments = commentDao.getCommentsByNewsId(news.getId());
        CommentAdapter commentAdapter = new CommentAdapter(this, comments);
        lvComments.setAdapter(commentAdapter);
    }

    private void submitComment() {
        String content = etComment.getText().toString().trim();
        String username = etUsername.getText().toString().trim();

        if (TextUtils.isEmpty(content)) {
            Toast.makeText(this, "请输入评论内容", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(username)) {
            Toast.makeText(this, "请输入您的昵称", Toast.LENGTH_SHORT).show();
            return;
        }

        Comment comment = new Comment();
        comment.setContent(content);
        comment.setNewsId(news.getId());
        comment.setUserName(username);

        long id = commentDao.addComment(comment);
        if (id > 0) {
            Toast.makeText(this, "评论成功", Toast.LENGTH_SHORT).show();
            etComment.setText("");
            loadComments();
        } else {
            Toast.makeText(this, "评论失败，请重试", Toast.LENGTH_SHORT).show();
        }
    }
} 